//
//  CityViewController.m
//  PerfectWorld
//
//  Created by dllo on 16/9/2.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "CityViewController.h"
#import "AFNetworking.h"
#import "AreaList.h"
#import "CityModel.h"
#import "CityTableViewCell.h"
#import "RightSearchViewController.h"

@interface CityViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)CityModel *cityModel;
@property (nonatomic, strong)UITableView *tableView;
@end

@implementation CityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(5, 20, 50, 35);
    [leftBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(clickLeftBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftBtn];
    [self creatTableView];
    [self handData];
}
- (void)handData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://api.guju.com.cn/v2/area/landingArea" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
        self.cityModel = [CityModel modelWithDic:dic];
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}
- (void)creatTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 99, self.view.bounds.size.width, self.view.bounds.size.height)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[CityTableViewCell class] forCellReuseIdentifier:@"cell"];
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(5, 64, self.view.bounds.size.width, 35)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, headerView.frame.size.width, headerView.frame.size.height)];
    label.text = @"热门";
    [headerView addSubview:label];
    [self.view addSubview:headerView];
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2, 20, 100, 35)];
    label2.text = @"热门城市";
    label2.textColor = [UIColor blackColor];
    [self.view addSubview:label2];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _cityModel.areaList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.areaList = _cityModel.areaList[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     // 传模型
     self.PassAreaList(_cityModel.areaList[indexPath.row]);
    [self dismissViewControllerAnimated:YES completion:^{       
    }];
}
- (void)clickLeftBtn:(UIBarButtonItem *)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
